<?php


function referToArray($refersitemap)
{
	$refersitemap = str_replace("][","@",$refersitemap);
	$refersitemap = str_replace("[","",$refersitemap);
	$refersitemap = str_replace("]","",$refersitemap);
	$arr = split("@",$refersitemap);
	return $arr;
}

function arrayToRefer($arr)
{
	$s="";
	foreach($arr as $item)
		$s.="[".$item."]";
	return $s;
}
function detail($nd,$n)
{
	$space=0;
	$i=0;
	for($i=0;$i<strlen($nd);$i++)
		if($nd[$i]==" ")
		{
			$space++;
			if($space==$n)
				break;
		}
	if($space==$n)
		return substr($nd,0,$i);
	else
		return $nd;
}
function arrsearch($arr,$value)
{
	if(count($arr)==0)
		return -1;
	foreach($arr as $key => $val)
		if($val==$value)
			return $key;
	return -1;
}
function matrixsearch($arr,$col,$value)
{
	for($i=0;$i<count($arr);$i++)
		if($arr[$i][$col]==$value)
			return $i;
	return -1;
}
function matrixexportarray($arr,$col)
{
	$ar=array();
	for($i=0;$i<count($arr);$i++)
		array_push($ar,$arr[$i][$col]);
			
	return $ar;
}
function removeArrValue($arr,$value)
{
	$temp=array();
	for($i=0;$i<count($arr);$i++)
		if($arr[$i]!=$value)
			array_push($temp,$arr[$i]);
	return $temp;
}
function existitem($arr1,$arr2)
{
	foreach($arr1 as $val1)
		foreach($arr2 as $val2)
			if($val1==$val2)
				return true;
	return false;
}
function travelArray($arr,&$result)
{
	if(is_array($arr))
		foreach($arr as $key => $val)
		{
			array_push($result,$key);
			if(count($val)>0)
				travelArray($val,$result);
		}
}

function dateFormat($datestring)
{
	$dt=new ClassDateTime();
	return $dt->_formatDateYMDtoDMY($datestring,"/");
}
function dateTimeFormat($datatimestring)
{
	$dt=new ClassDateTime();
	return $dt->_formatDateYMDtoDMY($datatimestring,"/")." ".$dt->_getTime($datatimestring);
}
function numberFormat($num,$length = 0)
{
	return number_format($num, $length, '.', ',');
}
function display($s)
{
	if($s=="")
		return "&nbsp;"; 
	else
		return $s;
}
function arrToList($arr,$c)
{
	if(count($arr)>0)
	{
		$list=array_shift($arr);
		foreach($arr as $key=>$val)
			if($val!="")
				$list.=$c.$val;
	}
	return $list;
}
function arrswap($arr)
{
	$ar=array();
	for($i=count($arr)-1;$i>=0;$i--)
		array_push($ar,$arr[$i]);
	return $ar;
}
function alert($mess)
{
	
	$s="<script language='javascript'>";
	$s.="alert('$mess')";
	$s.="</script>";
	echo $s;
}
function linkto($url)
{
	
	$s="<script language='javascript'>";
	$s.="window.location = '$url'";
	$s.="</script>";
	echo $s;
}
function writelog($conn,$user,$logaction)
{
	$log=new Log();
	$dt=new ClassDateTime();
	$flog=array(
				'THOIGIAN'=>$dt->_getNow("-"),
				'USERNAME'=>$user,
				'ACTION'=>$logaction
	);
	$log->insert($conn,$flog);
}
function advancedRmdir($path) 
{ 
    $origipath = $path;
    $handler = opendir($path);
    while (true) {
        $item = readdir($handler);
        if ($item == "." or $item == "..") {
            continue;
        } elseif (gettype($item) == "boolean") {
            closedir($handler);
            if (!@rmdir($path)) {
                return false;
            }
            if ($path == $origipath) {
                break;
            }
            $path = substr($path, 0, strrpos($path, "/"));
            $handler = opendir($path);
        } elseif (is_dir($path."/".$item)) {
            closedir($handler);
            $path = $path."/".$item;
            $handler = opendir($path);
        } else {
            unlink($path."/".$item);
        }
    }
    return true;
} 
function delFile($file)
{
	if(file_exists($file))
		unlink($file);
}
function replaceString($s,$arr)
{
	foreach($arr as $key => $val)
		$s=str_replace($key,$val,$s);
	return $s;
}
function strposlast($s,$need)
{
	for($i=strlen($s)-1;$i>=0;$i--)
		if($s[$i]==$need)
			return $i+1;
	return -1;
}
function queristring()
{
	//if(stripos($_SERVER['QUERY_STRING'],"page")==-1)
		//return $_SERVER['QUERY_STRING'];
	//echo stripos($_SERVER['QUERY_STRING'],"page");
	$flag=0;
	$arr=split("&",$_SERVER['QUERY_STRING']);
	//print_r($arr);
	for($i=0;$i<count($arr);$i++)
		if(stripos($arr[$i],"page")===true)
			$flag=1;
	if($flag==1)
		array_pop($arr);
	//print_r($arr);
	return $_SERVER['QUERY_STRING'];
}

function getURLQueryString($strkey, $setvalue)
{
	$address = $_SERVER['QUERY_STRING'];
	$arrParams = split("&", $address);
	if(count($arrParams)==1)
		return "?$address&$strkey=$setvalue";
	// Go though all elements
	$result = "?".$arrParams[0];
	for($i=1;$i<count($arrParams);$i++)
	{
		// replace already key
		$iskey = strpos($arrParams[$i], $strkey);
		if($iskey !== false) 
		{} 
		else 
		{ 
			if($i>0 && $result != '?') { $result .= "&"; }
			$result .= $arrParams[$i]; 
		}				
	}
	//
	if($result != '?') { $result .= "&"; }
	return $result .= "$strkey=$setvalue";
}

function loadClass($class)
{
	require('cms/modules/'.$class.'/'.$class.'class.php');
}
function loadClassCms($class)
{
	require('modules/'.$class.'/'.$class.'class.php');
}
$arrimageformate=array("jpg","gif","png");
$checknum="onkeyup='checknumber(this)'";
$headtable="align='left' bgcolor='#FF9900'";
$linetableheaderleft="style='border:1px; border-bottom:1px double #000000; border-left:1px double #000000; border-right:1px double #000000; border-top:1px double #000000'";
$linetableheader="style='border:1px; border-bottom:1px double #000000; border-right:1px double #000000; border-top:1px double #000000'";
$linetableleft="style='border:1px; border-bottom:1px dotted #000000; border-left:1px dotted #000000; border-right:1px dotted #000000'";
$linetable="style='border:1px; border-bottom:1px dotted #000000; border-right:1px dotted #000000'";
$vat=10;

function findTag($tag,$closetag,$positon,$s)
{
	$count=0;
	while($count < $positon)
	{
		
		$pos=strpos($s,$tag);
		$s=substr($s,$pos+strlen($tag));
		$count++;
	}
	$s=findCloseTag($tag,$closetag,$s);
	return substr($s,strpos($s,">")+1);
	
}

function findCloseTag($tag,$closetag,$s)
{
	$numtag=0;
	$find=strlen($s);
	for($i=0;$i<strlen($s);$i++)
	{
		$istag=false;
		$closetage=false;
		if($s[$i]==$tag[0])
		{
			$istag=true;
			for($j=0;$j<strlen($tag);$j++)
			{	
				if($s[$i+$j]!=$tag[$j])
					$istag=false;
			}
		}
		if($s[$i]==$closetag[0])
		{
			$closetage=true;
			for($j=0;$j<strlen($closetag);$j++)
			{
				if($s[$i+$j]!=$closetag[$j])
					$closetage=false;
			}
		}
		if($istag==true)
			$numtag++;
		if($closetage==true)
		{
			if($numtag > 0)
				$numtag--;
			else
			{
				$find=$i;
				break;
			}
		}
	}
	return substr($s,0,$find);	
}

function removeSubString($string,$start,$end)
{
	
	$poss = strpos($string,$start);
	$temp=substr($string,strpos($string,$start)+strlen($start));
	$pose=strpos($temp,$end) + strlen($start)+2;
	return str_replace(substr($string,$poss,$pose-1),"",$string);
}

function removeAll($string,$start,$end)
{
	while(strpos($string,$start)!== false)
	{
		$string=removeSubString($string,$start,$end);
	}
	return $string;
}

function getPrefix($letter,$n)
{
		$s="";
		for($i=0;$i<$n;$i++)
			$s.=$letter;
		return $s;
	}
?>